package UI;

import BLL.BasicInfoService;
import BLL.BookService;
import BLL.ReaderService;
import Entity.Books;
import Entity.Reads;
import Utils.TSUtility;

import java.sql.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @Author Chen
 * @Date 2022/1/6 17:40
 * @Version 1.0
 * 读者信息管理界面
 */
public class ReadersUI {
    public static void main(String[] args) {
        ui();
    }

    public static void ui() {
        Scanner sc = new Scanner(System.in);
        boolean option = true;
        while (option) {
            System.out.println("************图书管理系统***********");
            System.out.println("1.读者信息添加");
            System.out.println("2.读者信息修改");
            System.out.println("3.读者信息查询");
            System.out.println("4.读者信息删除");
            System.out.println("5.退出");
            System.out.println("请选择：");
            String number = sc.next();
            switch (number) {
                case "1":

                    System.out.println("**********图书管理系统***********");
                    System.out.println("************添加读者信息************");
                    System.out.println("请输入读者姓名：");
                    String readerName = TSUtility.readKeyBoard(4, false);
                    System.out.println("请输入出生年份：");
                    int year = TSUtility.readInt_year();
                    year = year - 1900;
                    System.out.println("请输入出生月份：");
                    int mouth = TSUtility.readInt_mouth();
                    mouth = mouth - 1;
                    System.out.println("请输入出生日期：");
                    int day = TSUtility.readInt_day();
                    Date publicationDate = new Date(year, mouth, day);
                    System.out.println("请输入性别：");
                    String sex = TSUtility.sex(1, false);
                    System.out.println("请输入电话号码：");
                    String phone = TSUtility.readPhone_11();
                    System.out.println("请输入所在院系：");
                    String department = TSUtility.readKeyBoard(20, false);
                    System.out.println("请输入注册年份：");
                    int year1 = TSUtility.readInt_year();
                    year = year - 1900;
                    System.out.println("请输入出生月份：");
                    int mouth1 = TSUtility.readInt_mouth();
                    mouth = mouth - 1;
                    System.out.println("请输入出生日期：");
                    int day1 = TSUtility.readInt_day();
                    Date regdate = new Date(year1, mouth1, day1);

                    BasicInfoService.showAllReaderType();
                    if (BasicInfoService.selectAllReaderType().size() == 0) {
                        System.out.println("请先添加读者类型：");
                        return;
                    }
                    boolean flog = true;
                    while (flog) {
                        System.out.println("请输入读者类型编号：");
                        int readerTypeNumber = TSUtility.readInt();
                        if (BasicInfoService.selectReaderTypeById(readerTypeNumber) != null) {
                            ReaderService.addReader(readerName, publicationDate, sex, phone, department, regdate, readerTypeNumber);
                            System.out.println("添加成功！");
                            flog = false;
                        } else {
                            System.out.println("你输入的读者类型编号不存在，请重新输入！");
                        }
                    }
                    break;
                case "2":
                    System.out.println("**********图书管理系统***********");
                    System.out.println("************修改读者信息************");
                    System.out.println("请输入要修改的读者ID：");
                    int readerNUmber = sc.nextInt();
                    Reads reads = ReaderService.selectReaderById(readerNUmber);
                    if (reads == null) {
                        System.out.println("没有为" + readerNUmber + "编号的读者信息！");
                        break;
                    }
                    System.out.println("请输入读者名称" + reads.getReadesName());
                    String name = TSUtility.readString(11, reads.getReadesName());
                    System.out.println("请输入出生日期(年)" + reads.getBirthday().getYear());
                    int year2 = Integer.parseInt(TSUtility.readString(4, String.valueOf(reads.getBirthday().getYear())));
                    year2 = year2 - 1900;
                    System.out.println("请输入出生日期(月)" + reads.getBirthday().getMonth());
                    int mouth2 = Integer.parseInt(TSUtility.readString(2, String.valueOf(reads.getBirthday().getMonth())));
                    mouth2 = mouth2 - 1;
                    System.out.println("请输入出生日期(日)" + reads.getBirthday().getDay());
                    int day2 = Integer.parseInt(TSUtility.readString(2, String.valueOf(reads.getBirthday().getDay())));
                    Date publicationDate1 = new Date(year2, mouth2, day2);
                    System.out.println("请输入读者的性别：" + reads.getSex());
                    String sex1 = TSUtility.readString(1, reads.getSex());
                    System.out.println("请输入电话号码：" + reads.getPhone());
                    String phone1 = TSUtility.readString(11, reads.getPhone());
                    System.out.println("请输入所在院系：" + reads.getDepartment());
                    String department1 = TSUtility.readString(11, reads.getDepartment());
                    System.out.println("请输入注册日期(年)：" + reads.getRegdate().getYear());
                    int year3 = Integer.parseInt(TSUtility.readString(4, String.valueOf(reads.getRegdate().getYear())));
                    year3 = year3 - 1900;
                    System.out.println("请输入注册日期(月)" + reads.getRegdate().getMonth());
                    int mouth3 = Integer.parseInt(TSUtility.readString(2, String.valueOf(reads.getRegdate().getMonth())));
                    mouth3 = mouth3 - 1;
                    System.out.println("请输入注册日期(日)" + reads.getRegdate().getDay());
                    int day3 = Integer.parseInt(TSUtility.readString(2, String.valueOf(reads.getRegdate().getDay())));
                    Date publicationDate2 = new Date(year3, mouth3, day3);
                    boolean flog1 = true;
                    while (flog1) {
                        BasicInfoService.showAllReaderType();
                        System.out.println("请输入读者类型编号：" + reads.getReadesTypeNumber());
                        int readerTypeNumber1 = Integer.parseInt(TSUtility.readString(6, String.valueOf(reads.getReadesTypeNumber())));
                        if (BasicInfoService.selectReaderTypeById(readerTypeNumber1) != null) {
                            ReaderService.updateReader(name, publicationDate1, sex1, department1, phone1, publicationDate2, readerTypeNumber1, readerNUmber);
                            System.out.println("修改成功！");
                            break;
                        } else {
                            System.out.println("没有该读者编号，请重新输入！");
                        }
                    }
                    break;
                case "3":
                    boolean flog2 = true;
                    while (flog2) {
                        System.out.println("**********图书管理系统***********");
                        System.out.println("************查询读者信息************");
                        System.out.println("1.读者信息全部查询");
                        System.out.println("2.指定读者编号查询");
                        System.out.println("3.指定读者名称查询");
                        System.out.println("4.指定读者类型查询");
                        System.out.println("5.退出");
                        System.out.println("请选择：");
                        String number1 = sc.next();
                        switch (number1) {
                            case "1":
                                ReaderService.selectAllReader();
                                break;
                            case "2":
                                System.out.println("请输入读者编号：");
                                int id = TSUtility.readInt();
                                if (ReaderService.selectReaderById(id) != null) {
                                    System.out.println(ReaderService.selectReaderById(id));
                                } else {
                                    System.out.println("该读者不存在！");
                                }
                                break;
                            case "3":
                                System.out.println("请输入读者姓名：");
                                String name1 = TSUtility.readKeyBoard(6, false);
                                if (ReaderService.selectReaderByName(name1) != null) {
                                    List<Reads> reads2 = ReaderService.selectReaderByName(name1);
                                    System.out.println(reads2);
                                } else {
                                    System.out.println("该读者不存在！");
                                }
                                break;
                            case "4":
                                System.out.println("请输入读者类型编号：");
                                int readerTypeNumber1 = TSUtility.readInt();
                                if (ReaderService.selectReaderType(readerTypeNumber1) != null) {
                                    List<Reads> reads1 = ReaderService.selectReaderType(readerTypeNumber1);
                                    System.out.println(reads1);
                                } else {
                                    System.out.println("没有该类型的读者！");
                                }
                                break;
                            case "5":
                                flog2 = false;
                                break;
                            default:
                                System.out.println("没有" + number1 + "这个选项！");
                                break;
                        }
                    }
                    break;
                case "4":
                    System.out.println("**********图书管理系统***********");
                    System.out.println("************删除读者信息************");
                    System.out.println("请输入要删除的读者编号：");
                    int id = TSUtility.readInt();
                    if (ReaderService.selectReaderById(id) != null) {
                        ReaderService.deleteReader(id);
                        System.out.println("删除成功！");
                    } else {
                        System.out.println("该读者不存在！");
                    }
                    break;
                case "5":
                    option = false;
                    break;
                default:
                    System.out.println("没有" + number + "这个选项！");
                    break;
            }
        }
    }
}
